package com.hp.bon.sgw.wh.channel.groupConsistency;

import java.io.IOException;
import java.util.Map;

import org.simpleframework.http.Query;
import org.simpleframework.http.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.invoices.bizSlot.HttpClientUtils;

public class ConsistencyRequestParser {

	private static final Logger _log = LoggerFactory.getLogger(ConsistencyRequestParser.class);

	public BizParam doParser(Request request) {
		BizParam param = new BizParam();
		String requestBody = HttpClientUtils.getRequestBody(request);
		/*requestBody=
				com.hp.bon.sgw.wh.channel.groupConsistency.utils.HttpClientUtils.getUTF8StringFromGBKString(requestBody);*/
		String requestPath = request.getPath().getPath();
		int index = requestPath.lastIndexOf("/");
		if (index >= 0) {
			requestPath = requestPath.substring(index + 1);
		}
		String requestMethod = request.getMethod();
		param.setParam(BizConsts.KEY_REQUEST_BODY, requestBody);
		param.setParam(BizConsts.KEY_REQUEST_PATH, requestPath);
		param.setParam(BizConsts.KEY_REQUEST_METHOD, requestMethod);
		String contentType = "";
		String appId = "";
		String appKey = "";
		String requestId = "";
		String regionId = "";
		Object lanId =null;
		if (!requestBody.isEmpty()) {
			Map<String, Object> requestBodyMap = HttpClientUtils.parseJSON2Map(requestBody);
			Map<String, Object> operAttrStruct = (Map<String, Object>) requestBodyMap.get("operAttrStruct");

			for (Map.Entry<String, Object> entry : operAttrStruct.entrySet()) {
				param.setParam(entry.getKey(),entry.getValue());
			}
		}
		Query query = request.getQuery();
		contentType = request.getValue(BizConsts.RQUEST_HEADER_CONTENT_TYPE);
		if (null == contentType) {
			contentType = query.get(BizConsts.RQUEST_HEADER_CONTENT_TYPE);
		}
		appId = request.getValue(BizConsts.RQUEST_HEADER_X_APP_ID);
		if (null == appId) {
			appId = query.get(BizConsts.RQUEST_HEADER_X_APP_ID);
		}
		appKey = request.getValue(BizConsts.RQUEST_HEADER_X_APP_KEY);
		if (null == appKey) {
			appKey = query.get(BizConsts.RQUEST_HEADER_X_APP_KEY);
		}
		requestId = request.getValue(BizConsts.RQUEST_HEADER_X_CTG_REQUEST_ID);
		if (null == requestId) {
			requestId = query.get(BizConsts.RQUEST_HEADER_X_CTG_REQUEST_ID);
		}
		regionId = request.getValue(BizConsts.RQUEST_HEADER_X_CTG_REGION_ID);
		if (null == regionId) {
			regionId = query.get(BizConsts.RQUEST_HEADER_X_CTG_REGION_ID);
		}
		lanId = param.getParam(BizConsts.KEY_LANID);
		if (null == lanId) {
			lanId = query.get(BizConsts.KEY_LANID);
		}

		param.setParam(BizConsts.RQUEST_HEADER_CONTENT_TYPE, contentType);
		param.setParam(BizConsts.RQUEST_HEADER_X_APP_ID, appId);
		param.setParam(BizConsts.RQUEST_HEADER_X_APP_KEY, appKey);
		param.setParam(BizConsts.RQUEST_HEADER_X_CTG_REQUEST_ID, requestId);
		param.setParam(BizConsts.RQUEST_HEADER_X_CTG_REGION_ID, regionId);
		param.setParam(BizConsts.KEY_LANID, lanId);
		
		_log.debug("param={}", JSON.toJSONString(param));

		return param;
	}

}
